<?php
namespace App\Http\Controllers\Client\Talent;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use App\Models\Tools\Period;
use App\Models\Work;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;

class TalentHistoryReceivedController extends ClientController
{

    /**
     * （达人）我的探店
     */
    public function index()
    {
        $all = $this->all;

        $pageSize = $all['pageSize'] ?? 10;
        $pageNo = $all['pageNo'] ?? 1;

        // 超时任务
        if(isset($all['overtime'])){
            sleep(1);
            Artisan::call('command:explore_overtime');
        }


        $now = time();

        $result = DB::table('explore_work as ew')
            ->leftJoin('explore_order as eo', 'ew.eo_id', '=', 'eo.eo_id')
            ->leftJoin('talent', 'ew.talent_id', '=', 'talent.talent_id')
            ->leftJoin('explore_channel as ec', 'eo.channel_id', '=', 'ec.channel_id')
            ->leftJoin('shop', 'ew.shop_id', '=', 'shop.shop_id')
            ->leftJoin('dict_area as da', 'shop.district', '=', 'da.id')
            ->where('ew.receiver_we_id', '=', $this->we_id)
            ->orderByDesc('ew.created_at')
            ->skip(($pageNo - 1) * $pageSize)
            ->take($pageSize)
            ->select('ew.work_id', 'ew.eo_id', 'ew.work_no', 'ew.status', 'ew.shop_id', 'ew.settlement_amount','ew.accept_remark','ew.hang_up','ew.created_at','ew.updated_at',
                'eo.channel_id', 'eo.start_date', 'eo.end_date', 'eo.offline', 'ec.channel_name', 'talent.account_name',
                'shop.shop_name', 'da.short_name', 'shop.address','shop.head_img')
            ->get()
            ->map(function ($item) use ($now) {
                $item->status_name = Data::$exploreWorkStatus[$item->status];
                $item->payment = $item->settlement_amount / 100;
                $detail_time = strtotime($item->created_at) + Period::getSetting('EXPLORE_UPLOAD_LIMIT_TIME');
                $item->detail_time = $now < $detail_time ? $detail_time-$now : null;
                $item->head_img = AliOss::getUrl($item->head_img);
                if($item->status === Data::DATA_DB_EXPLORE_WORK_STATUS_INIT){ // 初次接单，计算投稿剩余时间
                    $item->cancel_at = Period::getSetting('EXPLORE_UPLOAD_OVERTIME') - ($now - strtotime($item->updated_at));
                }
                if($item->status === Data::DATA_DB_EXPLORE_WORK_STATUS_REFUSE){ // 验收不通过，计算投稿剩余时间
                    // 截止日期和最后更改日期最大值+2天
                    $end_date = max(strtotime($item->end_date.'00:00:00'),strtotime($item->updated_at)) + Period::getSetting('EXPLORE_UPLOAD_OVERTIME');
                    $item->cancel_at = $end_date - time();
                }
                return $item;
            })
            ->toArray();
        return $this->setReturnJson(0, 'success', $result);

    }

    /**
     * 日志
     */
    public function log()
    {
        return $this->setReturnJson(0,'success',Work::workLog($this->all['work_id']));
    }

}
